package daos;
import com.j256.ormlite.dao.*;
import com.j256.ormlite.stmt.*;
import com.j256.ormlite.support.*;
import entities.*;
import java.util.*;
import java.util.logging.*;
import java.util.logging.Logger;
/**
 * This Class is used to handle 
 * the DAO methods of the User class 
 * @author Juanan JS  juanantonios@gmail.com
 */
public class UserDao
{
    private ConnectionSource objConnectionSource=null;
    /**
     * Constructor:
     * @param objConnectionSource_ 
     * An active instance of a ConnectionSource
     */
    public UserDao(ConnectionSource objConnectionSource_)
    {
        this.objConnectionSource=objConnectionSource_;
    }
    
    /**
    * @param strLogin Login name of a existent user
    * @return An Object of UsrUser or null 
    * if the login given doesn't exists
    */ 
    public UsrUser findByLogin(String strLogin)
    {
        UsrUser objUser=null;
        try
        {
            //JS: do the user dao
            Dao<UsrUser, String> objUserDao = DaoManager.createDao(objConnectionSource, UsrUser.class);
            //JS: do the query for user login
            QueryBuilder<UsrUser, String> queryBuilder =objUserDao.queryBuilder();
            queryBuilder.where().eq(UsrUser.USR_LOGIN, strLogin);
            PreparedQuery<UsrUser> preparedQuery = queryBuilder.prepare();
            //JS: query for all accounts that have that login
            List<UsrUser> objUsrList = objUserDao.query(preparedQuery);
            //JS: Create the User Object
            objUser = (UsrUser) objUsrList.get(0);
        } catch (Exception ex)
        {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return objUser;
    }

}
